Ext.ns('journal.ui');

journal.ui.TreeFilter = Ext.extend(Ext.ux.tree.TreeFilterX, {
    // {{{
    /**
     * Filter the data by a specific attribute.
     * 
     * @param {String/RegExp} value Either string that the attribute value should start with or a RegExp to test against
     *            the attribute
     * @param {String} attr (optional) The attribute passed in your node's attributes collection. Defaults to "text".
     * @param {TreeNode} startNode (optional) The node to start the filter at.
     */
    filter : function(value, attr, startNode) {

        // expand start node
        if (false !== this.expandOnFilter) {
            startNode = startNode || this.tree.root;
            var animate = this.tree.animate;
            this.tree.animate = false;
            startNode.expand(true, false, function() {

                // call parent after expand
                attr = attr || "text";
                var f;
                if (typeof value == "string") {
                    var vlen = value.length;
                    // auto clear empty filter
                    if (vlen == 0 && this.clearBlank) {
                        this.clear();
                        return;
                    }
                    value = value.toLowerCase();
                    f = function(n) {
                        return n.attributes[attr].substr(0, vlen).toLowerCase() == value;
                    };
                } else if (value.exec) { // regex?
                    f = function(n) {
                        var retval = value.test(n.attributes[attr]);
                        retval = retval || value.test(n.parentNode.attributes[attr]);
                        while (!retval && parent != null && (parent = parent.parentNode) != null) {
                            retval = retval || value.test(parent.attributes[attr]);
                        }
                        return retval;
                    };
                } else {
                    throw 'Illegal filter type, must be string or regex';
                }
                this.filterBy(f, null, startNode);
            }.createDelegate(this));
            this.tree.animate = animate;
        } else {
            // call parent
            Ext.ux.tree.TreeFilterX.superclass.filter.apply(this, arguments);
        }
    } // eo function filter
    // }}}
}); // eo extend

// eof
